home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Hot Mix 14
/
Hot Mix 14.iso
/
HTML
/
vendors
/
3Name3D
/
TickerTape.java
< prev
next >
Wrap
Text File
|
1996-07-22
|
7KB
|
183 lines
* TickerTape - Ticker tape program written in java
* I guess this is pretty much becoming the "Hello World" program
* for Java. This is the basic TickerTape java program with the
* added feature of selecting and entry and connectioning to an
* associated URL. There is much that can be added.
* Patrick Palmer, 1/3/96
*/
import java.applet.Applet;
import java.awt.*;
import java.lang.*;
import java.net.*;
public class TickerTape extends Applet implements Runnable {
private final static int NUM_TEXT = 10;
private int delay = 100;
private int offset = 0;
private int width;
private int twidth = 0;
private int height;
private String text = null;
private String[] url;
private int[] url_off;
private int url_count = 0;
private Thread thread = null;
private int baseline;
private Image image;
private Font font;
private int step = 3;
public void init() {
int fsize = 18; // default font size
String str, arg;
int count;
setBackground(Color.black);
setForeground(Color.white);
// get parameters
if ((str = super.getParameter("size")) != null)
fsize = Integer.valueOf(str).intValue();
if ((str = super.getParameter("font")) == null)
str = "TimesRoman";
this.font = new Font(str, Font.PLAIN, fsize);
if ((str = super.getParameter("delay")) != null)
this.delay = Integer.valueOf(str).intValue();
if ((str = super.getParameter("step")) != null)
this.step = Integer.valueOf(str).intValue();
if ((str = super.getParameter("count")) != null)
count = Integer.valueOf(str).intValue();
else
count = NUM_TEXT;
// get height and width
this.width = super.size().width;
this.height = super.size().height;
// set up secondary image
this.image = createImage(this.width, this.height);
// set up objects
this.url = new String[count];
this.url_off = new int[count];
FontMetrics fm = image.getGraphics().getFontMetrics(this.font);
this.baseline = fm.getHeight() - fm.getDescent();
// read in text lines
for (int i = 0; i < count; i++) {
arg = "text" + i;
if ((str = super.getParameter(arg)) != null) {
// save text
if (this.text == null)
this.text = str;
else
this.text = this.text + str;
// get the pixel width of the string
this.twidth = fm.stringWidth(text);
this.url_off[this.url_count] = twidth;
// get the url address
arg = "url" + i;
this.url[this.url_count] = super.getParameter(arg);
this.url_count++;
}
}
}
public final synchronized void update(Graphics g) {
int off;
// make sure there is text
if (this.text == null)
return;
// draw
Graphics gfx = this.image.getGraphics();
gfx.setFont(this.font);
gfx.setColor(super.getBackground());
gfx.fillRect(0, 0, this.width, this.height);
gfx.setColor(super.getForeground());
gfx.drawString(this.text, this.width - this.offset, this.baseline);
g.drawImage(this.image, 0, 0, null);
// move
this.offset += this.step;
if (this.offset >= this.twidth + this.width)
this.offset = 0;
}
public boolean mouseDown(Event evt, int x, int y) {
int off = (-(this.width - this.offset)) + x;
if (off < 0 || off > this.twidth)
return true;
int entry = 0;
int begin = 0;
for (int i = 0; i < this.url_count; i++)
if (off >= begin && off < this.url_off[i]) {
entry = i;
begin = this.url_off[i];
}
if (url[entry] != null) {
try {
super.getAppletContext().showDocument(new URL(url[entry]));
} catch(MalformedURLException e) {
System.out.println("Invalid address " + url[entry]);
}
if (url[entry].substring(0,6).compareTo("mailto") != 0)
this.stop();
}
return true;
}
public void run() {
while(this.thread != null) {
try { Thread.sleep(this.delay); }
catch (InterruptedException e) { }
super.repaint();
}
}
public void start() {
if (this.thread == null) {
this.thread = new Thread(this);
this.thread.start();
}
}
public void stop() {
if (this.thread != null) {
this.thread.stop();
this.thread = null;
}
}
public String getAppletInfo() {
return "yet another TickerTape.java";
}
public String[][] getParameterInfo() {
String[][] i = {
{"font", "string", "Font (def TimesRoman)" },
{"size", "int", "Font Size (def 18)" },
{"delay", "int", "Scroll delay in ms (def 100)" },
{"step", "int", "Scroll pixel step (def 3)" },
{"count", "int", "Number of text messages (def 10)" },
{"text0", "string", "Text message 0" },
{"textn", "string", "Text message #n" },
{"url0", "string", "URL 0 associated with text0" },
{"urln", "string", "URL #n associated with textn" }
};
return i;
}
}